package jmine.tec.datadigester.positional;

import static junit.framework.Assert.fail;
import jmine.tec.datadigester.positional.meta.PositionalFieldMetadata;
import jmine.tec.datadigester.positional.meta.PositionalLayoutMetadataFactory;
import jmine.tec.datadigester.positional.meta.PositionalLineMetadata;

/**
 * @author israel.crisanto
 */
public class PositionalLayoutStaticTester {

    private PositionalLayoutMetadataFactory factory = new PositionalLayoutMetadataFactory();

    public void checkPositionalBeanAnnotations(Class<?> beanClass) {
        this.checkForOverlappingAnnotations(this.factory.forBean(beanClass));
    }

    /**
     * Verifica se os dados de layout informados são coerentes e não estão sobrepostos
     * 
     * @param annotatedClass
     */
    private void checkForOverlappingAnnotations(PositionalLineMetadata line) {
        int previousEnd = 0;
        for (PositionalFieldMetadata field : line.getFields()) {
            if (field.start() < previousEnd) {
                fail("Field overlapping with previous: " + field.name());
            }
            previousEnd = field.start() + field.size();
        }
    }
}
